﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Instrument;

namespace UI
{
    public partial class PersonInfo : DevExpress.XtraEditors.XtraForm
    {
        public PersonInfo(string state)
        {
            InitializeComponent();
            stateFlag = state;
        }
        public PersonInfo(string state,DataRow dataRow)
        {
            InitializeComponent();
            stateFlag = state;
            JobNum = dataRow[0].ToString();
            this.Text = "正在修改员工：" + JobNum;
            txtJobNum.EditValue = dataRow[0];
            txtName.EditValue = dataRow[1];
            txtPassWord.EditValue = dataRow[2];
            cbbRole.SelectedIndex = (dataRow[3].ToString() == "Manager") ? 0 : 1;
        }


        string stateFlag;//确定窗体执行修改还是增加功能
        string JobNum;

        private void simpleButton1_Click_1(object sender, EventArgs e)
        {
            Employee employee = new Employee();
            if (txtJobNum.EditValue == null || cbbRole.EditValue == null || txtPassWord.EditValue == null
                || txtName.EditValue == null)
            {
                MessageBox.Show("信息不全");
                return;
            }
            employee.JobNum = txtJobNum.EditValue.ToString();
            employee.UserName = txtName.EditValue.ToString();
            employee.PassWord = txtPassWord.EditValue.ToString();
            employee.Role = (cbbRole.SelectedItem.ToString() == "管理员") ? "Manager" : "Ordinary";
            switch (stateFlag)
            {
                case "New":
                    if( DataBaseOperation.GetInstance().InsertEmployee(employee) == false)
                    {
                        MessageBox.Show("操作失败");
                        return;
                    }
                    Close();
                    break;
                case "Modify":
                    if(DataBaseOperation.GetInstance().UpdateOneEmployee(JobNum, employee) == false)
                    {
                        MessageBox.Show("操作失败");
                        return;
                    }
                    Close();
                    break;
            }
        }
    }
}